//
//  UserInfo.m
//  AFNetworking
//
//  Created by admin on 2020/5/13.
//

#import "UserInfo.h"

static UserInfo *userInfo = nil;

@implementation UserInfo

+(UserInfo *)share{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        userInfo = [UserInfo new];
        [[NSNotificationCenter defaultCenter] addObserver:userInfo selector:@selector(applicationDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification  object:nil];
        NSDictionary *userinfoDic = [[NSUserDefaults standardUserDefaults]objectForKey:@"store.userinfo"];
        if (userinfoDic) {
            [userInfo modelSetWithJSON:userinfoDic];
        }
    });
    return userInfo;
}

-(void)update:(void(^)(NSError *error))endblock{
    
    NSString *url = [NSString stringWithFormat:@"%@/sso/myInfo", BASEURL];
    [[Network share] sendGet:url param:@{} head:nil block:^(NSDictionary * _Nonnull result, NSError * _Nonnull error) {
        if(!error){
            [self modelSetWithJSON:result];
        }
        endblock(error);
    }];
}
-(void)save{
    id save = [self modelToJSONObject];
    [[NSUserDefaults standardUserDefaults] setObject:save forKey:@"store.userinfo"];
}

-(void)applicationDidEnterBackground{
    [self save];
}

@end
